#include "Component/Item/Light/ElectricSwitchComponent.h"

#include "Component/Item/Light/RoomSwitchComponent.h"
#include "Subsystem/ItemManager/RoomLightManagerSubsystem.h"
#include "Subsystem/RoomManager/RoomManagerFunctionLibrary.h"

void UElectricSwitchComponent::BeginPlay()
{
	Super::BeginPlay();

	if (URoomLightManagerSubsystem* Subsystem = URoomManagerFunctionLibrary::GetRoomLightManagerSubsystem(this))
	{
		Subsystem->RegisterElectricSwitch(GetOwner());
	}
}

void UElectricSwitchComponent::EndPlay(const EEndPlayReason::Type EndPlayReason)
{
	if (URoomLightManagerSubsystem* Subsystem = URoomManagerFunctionLibrary::GetRoomLightManagerSubsystem(this))
	{
		Subsystem->UnregisterElectricSwitch(GetOwner());
	}

	Super::EndPlay(EndPlayReason);
}

TArray<URoomSwitchComponent*> UElectricSwitchComponent::GetRoomSwitchComponents()
{
	if (RoomSwitchComponents.IsEmpty())
	{
		const FName RoomName = URoomManagerFunctionLibrary::GetActorRoomName(this, GetOwner());
		if (URoomLightManagerSubsystem* RoomLightManagerSubsystem = URoomManagerFunctionLibrary::GetRoomLightManagerSubsystem(this))
		{
			RoomSwitchComponents = RoomLightManagerSubsystem->GetRoomComponents<URoomSwitchComponent>(RoomName);
		}
	}

	return RoomSwitchComponents;
}
